/*
 * @Descripttion:
 * @version:
 * @Author: ChenXiang
 * @Date: 2021-03-22 16:18:10
 * @LastEditors: ChenXiang
 * @LastEditTime: 2021-09-08 14:32:58
 */

/**
 * storage.set(_url, JSON.stringify(response), 60 * 12 * 7);默认七天有效期
 */

class Storage {
  props: any;
  source: any;
  constructor(props?: any) {
    this.props = props || {};
    this.source = this.props.source || window.localStorage;
    this.initRun();
  }

  /*
 * set 存储方法
 * @ param {String} key 键
 * @ param {String} value 值，存储的值可能是数组/对象，不能直接存储，需要转换 JSON.stringify
 * @ param {String} expired 过期时间，以分钟为单位
 */
  initRun() {
    const reg = new RegExp("__expires__");
    const data = this.source;
    const list = Object.keys(data);
    if (list.length > 0) {
      list.map((key, v) => {
        if (!reg.test(key)) {
          const now = Date.now();
          const expires = data[`${key}__expires__`] || Date.now() + 1;
          if (now >= expires) {
            this.remove(key);
          }
        }
        return key;
      });
    }
  }


  /*
   * get 获取方法
   * @ param {String} key 键
   * @ param {String} expired 存储时为非必须字段，所以有可能取不到，默认为 Date.now+1
   */
  get(key: string) {
    const source = this.source;
    const expired: any = source[`${key}__expires__`] || Date.now() + 1;
    const now = Date.now();

    if (now >= expired) {
      this.remove(key);
      return;
    }
    const value = source[key] ? JSON.parse(source[key]) : source[key];
    return value;
  }

  /*
   * set 存储方法
   * @ param {String} key 键
   * @ param {String} value 值，
   * @ param {String} expired 过期时间，以分钟为单位，非必须
   */
  set(key: string, value: any, expired?: number) {
    const source = this.source;
    source[key] = JSON.stringify(value);
    if (expired) {
      source[`${key}__expires__`] = Date.now() + 1000 * 60 * expired;
    }
    return value;
  }

  remove(key: string) {
    const data = this.source;
    const value = data[key];
    delete data[key];
    delete data[`${key}__expires__`];
    return value;
  }
}

export default new Storage();
